<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>账号安全 - TechFolio 个人项目展示网站</title>
    <link rel="stylesheet" href="/css/element-ui.css">
    <link rel="stylesheet" href="/css/common.css">
    <script src="/js/vue.js"></script>
    <script src="/js/element-ui.js"></script>
    <script src="/js/axios.min.js"></script>
    <script src="/js/qs.js"></script>
</head>
<body>
<div id="app">
    <el-container>
        <!-- 头部 -->
        <el-header class="layout-header" style="">
            <div class="title">
                <h1>TechFolio 个人项目展示网站 v1.0.0</h1>
            </div>
            <el-breadcrumb separator-class="el-icon-arrow-right"
                           style="height: 48px; line-height: 48px; font-size: 16px;">
                <el-breadcrumb-item>后台管理</el-breadcrumb-item>
                <el-breadcrumb-item>账号安全</el-breadcrumb-item>
            </el-breadcrumb>
        </el-header>
        <!-- 主体 -->
        <el-container class="layout-body">
            <!-- 左侧 -->
            <el-aside width="80px" class="layout-aside">
                <div class="menu-item">
                    <a href="profile.html">
                        <div><i class="el-icon-user"></i></div>
                        <div class="menu-item-text">个人资料</div>
                    </a>
                </div>
                <div class="menu-item">
                    <a href="project.html">
                        <div><i class="el-icon-document-copy"></i></div>
                        <div class="menu-item-text">项目管理</div>
                    </a>
                </div>
                <div class="menu-item">
                    <a href="security.html" class="active">
                        <div><i class="el-icon-lock"></i></div>
                        <div class="menu-item-text">账号安全</div>
                    </a>
                </div>
                <div class="menu-item">
                    <a @click="logout">
                        <div><i class="el-icon-switch-button"></i></div>
                        <div class="menu-item-text">退出登录</div>
                    </a>
                </div>
            </el-aside>
            <!-- 右侧 -->
            <el-main class="layout-main">
                <!-- 操作区域 -->
                <div class="operate-area">
                    <h1>账号安全</h1>
                </div>
                <!-- 表单 -->
                <div>
                    <el-card class="common-card" style="width: 500px;">
                        <div slot="header" class="clearfix">
                            <span>修改密码</span>
                        </div>
                        <div style="padding: 0px 20px;">
                            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px"
                                     label-position="left"
                                     class="demo-ruleForm">
                                <el-form-item label="原密码" prop="oldPassword">
                                    <el-input v-model="ruleForm.oldPassword" type="password"></el-input>
                                </el-form-item>
                                <el-form-item label="新密码" prop="newPassword">
                                    <el-input v-model="ruleForm.newPassword" type="password"></el-input>
                                </el-form-item>
                                <el-form-item label="确认密码" prop="confirmPassword">
                                    <el-input v-model="ruleForm.confirmPassword" type="password"></el-input>
                                </el-form-item>
                                <el-form-item>
                                    <el-button type="primary" class="btn-submit" @click="submitForm('ruleForm')">修改
                                    </el-button>
                                </el-form-item>
                            </el-form>
                        </div>
                    </el-card>
                </div>
            </el-main>
        </el-container>
    </el-container>
</div>

<script>
    new Vue({
        el: '#app',
        data: {
            ruleForm: {
                oldPassword: '',
                newPassword: '',
                confirmPassword: ''
            },
            rules: {
                oldPassword: [
                    {required: true, message: '请输入原密码', trigger: 'blur'},
                    {min: 4, max: 16, message: '密码必须是4~16位', trigger: 'blur'}
                ],
                newPassword: [
                    {required: true, message: '请输入新密码', trigger: 'blur'},
                    {min: 4, max: 16, message: '密码必须是4~16位', trigger: 'blur'}
                ],
                confirmPassword: [
                    {required: true, message: '请输入确认密码', trigger: 'blur'},
                    {min: 4, max: 16, message: '密码必须是4~16位', trigger: 'blur'}
                ]
            }
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        if (this.ruleForm.newPassword != this.ruleForm.confirmPassword) {
                            this.$alert('新密码与确认密码不一致', '错误', {
                                confirmButtonText: '确定',
                                callback: action => {
                                }
                            });
                            return false;
                        }

                        let url = '/api/admin/password/update';
                        let params = Qs.stringify(this.ruleForm);
                        axios.post(url, params).then(response => {
                            let jsonResult = response.data;
                            if (jsonResult.state == 20000) {
                                this.$message({
                                    type: 'success',
                                    message: '修改成功，请牢记您的新密码：' + this.ruleForm.newPassword
                                });
                                this.resetForm();
                            } else if (jsonResult.state === 40100) {
                                this.$alert(jsonResult.message, '错误', {
                                    confirmButtonText: '确定',
                                    callback: action => {
                                        window.location.href = 'login.html';
                                    }
                                });
                            } else {
                                this.$alert(jsonResult.message, '错误', {
                                    confirmButtonText: '确定',
                                    callback: action => {
                                    }
                                });
                            }
                        }).catch(error => {
                            this.$alert('服务器端无法正确响应【修改密码】功能！', '错误', {
                                confirmButtonText: '确定',
                                callback: action => {
                                }
                            });
                        });
                    } else {
                        this.$alert('请正确填写数据！', '错误', {
                            confirmButtonText: '确定',
                            callback: action => {
                            }
                        });
                        return false;
                    }
                });
            },
            resetForm() {
                this.ruleForm.oldPassword = '';
                this.ruleForm.newPassword = '';
                this.ruleForm.confirmPassword = '';
            },
            // 退出登录
            logout() {
                let message = '登录信息将被注销，在重新登录之前不可进行后台管理操作，是否继续？';
                this.$confirm(message, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let url = '/api/admin/logout';
                    axios.post(url).then(response => {
                        let jsonResult = response.data;
                        if (jsonResult.state === 20000) {
                            window.location.href = '/';
                        } else {
                            this.$alert(jsonResult.message, '错误', {
                                confirmButtonText: '确定',
                                callback: action => {
                                    window.location.href = 'login.html';
                                }
                            });
                        }
                    }).catch(error => {
                        this.$alert('服务器端无法正确响应【退出登录】功能！', '错误', {
                            confirmButtonText: '确定',
                            callback: action => {
                            }
                        });
                    });
                }).catch(() => {
                });
            }
        },
        mounted() {
        }
    });
</script>
</body>
</html>